package org.david.code.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
/**
 * 
 */
public class UploadFile {
    private static Logger logger = Logger.getLogger(UploadFile.class);
   
    /**
     * 获取文件扩展名
     * @param fileName
     * @return
     */
    public static String getExtName(String fileName) {
        try {
            if (fileName == null || fileName.trim().length() == 0)
                return "";
            return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        }
        catch (Exception e) {
            logger.error("获取" + fileName + "文件的扩展名错", e);
        }
        return "";
    }
    /**
	 * @param byteFile
	 * @param fileName
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	
	private File saveFile(byte[] byteFile, String fileName)
			throws FileNotFoundException, IOException {
		InputStream in = null;
		OutputStream out = null;
		File file = new File("d:/uploadtest/", fileName);
		in = new ByteArrayInputStream(byteFile); // 打开到这个URL的流
		out = new FileOutputStream(file);

		// 复制字节到输出流
		byte[] buffer = new byte[4096];
		int bytes_read;
		while ((bytes_read = in.read(buffer)) != -1) {
			out.write(buffer, 0, bytes_read);
		}

		in.close();
		out.close();
		return file;
	}
}
